#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"

cmd-tags-destroy() {
  declare desc="destroys an app image tag via command line"
  declare cmd="tags:destroy"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" IMAGE_TAG="$2"

  [[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
  verify_app_name "$APP"

  local IMAGE_REPO=$(get_app_image_repo "$APP")
  case "$IMAGE_TAG" in
    latest)
      dokku_log_fail "You can't remove internal dokku tag ($IMAGE_TAG) for $IMAGE_REPO"
      ;;

    *)
      local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
      plugn trigger scheduler-tags-destroy "$DOKKU_SCHEDULER" "$APP" "$IMAGE_REPO" "$IMAGE_TAG"
      ;;
  esac
  plugn trigger tags-destroy "$APP" "$IMAGE_TAG"
}

cmd-tags-destroy "$@"
